package com.jijuxie.house.controller;

import com.jijuxie.common.core.R;
import com.jijuxie.house.domain.dto.CollectCountDTO;
import com.jijuxie.house.domain.UserHouseCollect;
import com.jijuxie.house.mapper.UserHouseCollectMapper;
import com.jijuxie.house.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import cn.dev33.satoken.stp.StpUtil;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 房源收藏控制器
 */
@RestController
@RequestMapping("/house/collect")
public class HouseCollectController {

    @Autowired
    private IHouseService houseService;
    
    @Autowired
    private UserHouseCollectMapper userHouseCollectMapper;

    /**
     * 按条件统计收藏数量
     */
    @PostMapping("/count")
    public R<Integer> countCollects(@RequestBody CollectCountDTO dto) {
        Integer count = houseService.countCollects(dto);
        return R.ok(count);
    }

    /**
     * 按时间段统计收藏数量
     */
    @GetMapping("/count/date-range")
    public R<List<Map<String, Object>>> countCollectsByDateRange(
            @RequestParam Long userId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        List<Map<String, Object>> result = houseService.getDailyCollectCounts(userId, startTime, endTime);
        return R.ok(result);
    }

    /**
     * 获取收藏趋势数据
     */
    @GetMapping("/trend")
    public R<Map<String, Object>> getCollectTrend(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "7") Integer days) {
        // 计算时间范围
        LocalDateTime endTime = LocalDateTime.now();
        LocalDateTime startTime = endTime.minusDays(days);
        
        // 获取每日收藏数量
        List<Map<String, Object>> dailyCounts = houseService.getDailyCollectCounts(userId, startTime, endTime);
        
        // 获取总收藏数
        CollectCountDTO dto = new CollectCountDTO();
        dto.setUserId(userId);
        dto.setStatus(1);
        Integer totalCount = houseService.countCollects(dto);
        
        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("dailyCounts", dailyCounts);
        result.put("totalCount", totalCount);
        result.put("startDate", startTime);
        result.put("endDate", endTime);
        
        return R.ok(result);
    }

    @PostMapping("/collect/{houseId}")
    public R<Map<String, Object>> collect(@PathVariable Long houseId) {
        Long userId = StpUtil.getLoginIdAsLong();
        
        UserHouseCollect collect = new UserHouseCollect();
        collect.setUserId(userId);
        collect.setHouseId(houseId);
        collect.setCollectTime(LocalDateTime.now());  // 设置了收藏时间
        collect.setCreateTime(LocalDateTime.now());
        collect.setUpdateTime(LocalDateTime.now());
        collect.setStatus("1");
        
        userHouseCollectMapper.insert(collect);
        
        Map<String, Object> result = new HashMap<>();
        result.put("collectTime", collect.getCollectTime());  // 返回了收藏时间
        result.put("isCollected", true);
        
        return R.ok(result);
    }
} 